<?php
/**
 * Name: 角色控制器.
 * User: 董坤鸿
 * Date: 2016/10/13
 * Time: 10:43
 */

namespace App\Http\Controllers\Resources;
use App\Models\Role;
use App\Services\ServiceFactory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\RoleRequest;
use App\Services\Interfaces\RepoServiceInterface;
use Auth;

class RoleController extends Controller
{
    /**
     * 初始化
     * RoleController constructor.
     * @param ServiceFactory $_repoService
     */
    public function __construct($_resource)
    {
        $this->_resource = $_resource;
    }

    /**
     * 角色列表
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //当前登录用户为操作员的话,无权限创建
        //$this->authorize(new Role());
        $data = $this->_repoService()->read();
        if (!empty($data)) {
            return ['total' => count($data), 'data'=>$data];
        } else {
            return ['total' => 0, 'data'=>[]];
        }
    }

    /**
     * 新增角色
     * Show the form for creating a new resource.
     * @return \Illuminate\Http\Response
     * @param RoleRequest $request
     */
    public function create()
    {
        //当前登录用户为操作员的话,无权限创建
        $this->authorize('create', new Role());
    }

    /**
     * 插入角色
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return RoleRequest $request
     */
    public function store(RoleRequest $request)
    {
        $data = $request->all();
        $user = request()->user();  //获取当前登录用户信息
        $count = Role::where(['creator' => $user->id, 'title' => $data['title']])->count();
        if (empty($count) && !empty($data['title'])) {
            $result = $this->_repoService()->create($data);
            return $result;
        } else {
            return $this->_error('角色已经存在');
        }
    }

    /**
     * 角色详情
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //当前登录用户为操作员的话,无权限创建
        //$this->authorize('show', new Role());
        $result = $this->_repoService()->find(['id' => $id]);
        if ($result) {
            return $result;
        } else {
            return $this->_error('没有指定的角色');
        }
    }

    /**
     * 编辑角色
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //当前登录用户为操作员的话,无权限创建
        $this->authorize('edit', new Role());
    }

    /**
     * 修改角色
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Request
     */
    public function update(RoleRequest $request, $id)
    {
        $data = $request->all();
        $user = request()->user();  //获取当前登录用户信息
        $count = Role::where(['creator' => $user->id, 'title' => $data['title']])
            ->whereNotIn('_id', [$id])
            ->count();
        if (!empty($count)) {
            return $this->_error('角色已经存在');
        }
        $isSave = $this->_repoService()->update(['id' => $id], $data);
        if ($isSave) {
            $result = $this->_repoService()->find(['id' => $id]);
            return $result;
        } else {
            return $this->_error("修改失败");
        }
    }

    /**
     * 删除角色
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //当前登录用户为操作员的话,无权限创建
        $this->authorize('destroy', new Role());
        $result = $this->_repoService()->delete(['id' => $id]);
        if ($result) {
            return "delete success";
        } else {
            return $this->_error("删除失败.只能删除未绑定账号的角色");
        }
    }
}
